package com.chenchao.javapackaging;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/10 13:12
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javapackaging.Boxing2.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class Boxing2 {
    public static void main(String[] args) {
        //包装类的缓存设计
        Integer integer1 = new Integer(1);
        Integer integer2 = new Integer(1);
        //直接开辟内存空间，肯定不相同
        System.out.println(integer1 == integer2);//false

        Integer integer3 = Integer.valueOf(1);
        Integer integer4 = Integer.valueOf(1);
        Integer integer5 =1;

        System.out.println(integer3 == integer4);//true
        System.out.println(integer3 == integer5);//true
        //This method will always cache values in the range -128 to 127,
        Integer integer6 = Integer.valueOf(128);
        Integer integer7 = Integer.valueOf(128);
        System.out.println(integer6 == integer7);//flase
        //超出缓存范围，不会再用缓存，直接创建地址
        //此时相当于Integer integer6 = new Integer(128);

        Character character1 = Character.valueOf((char) 128);
        Character character2 = Character.valueOf((char) 128);
        System.out.println(character1 ==character2);//false
        //Character的范围是[0,127]
    }
}
